<template>
  <div class="app-container">
    <h2 style="text-align: center;">项目名称:{{$route.query.name}}</h2>
    <el-button type="text" plain @click="openDetail" style="font-size: 20px;"><span style="color: black;">文件:</span>{{$route.query.pdfName}}</el-button>
    <!-- 弹出框 -->
    <el-dialog
      :title="$route.query.pdfName"
      @close="onClose"
      :visible.sync="openPdf"
      :close-on-click-modal="false"
      width="95%"
      style="margin: -2% auto 0;overflow: hidden">
      <!-- 预览pdf文件 -->
      <!--      :width="screenWidth"-->
      <embed
        :src="$route.query.url"
        type="application/pdf"
        width="100%"
        :height="screenHeight-100"
      />
<!--      :width="screenWidth"-->

    </el-dialog>


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button icon="el-icon-d-arrow-left"  type="primary" size="mini" @click="back">返回</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:car:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:car:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:car:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      stripe
      v-loading="loading"
      :data="carList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车号" align="center" prop="number" />
      <el-table-column label="车型" align="center" prop="type" />
      <el-table-column label="是否排查" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === '未排查'">未排查</span>
          <span v-else style="color: red">已排查</span>
        </template>
      </el-table-column>
      <el-table-column label="排查时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查班组" align="center" prop="checkTeam" />
      <el-table-column label="排查人员" align="center" prop="checkPeople" />
      <el-table-column label="是否发现故障" align="center" prop="isFound"/>
      <el-table-column label="故障信息" align="center" prop="information">
        <template slot-scope="scope">
          <span v-if="scope.row.isFound === '未发现'">无</span>
          <span v-if="scope.row.isFound === '已发现'">{{scope.row.information}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:car:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="number">
          <el-input v-model="form.number" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车型" prop="type">
          <el-input v-model="form.type" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="排查时间" prop="time">
          <el-date-picker clearable
                          v-model="form.time"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择排查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查班组" prop="checkTeam">
          <el-input v-model="form.checkTeam" placeholder="请输入检查班组" />
        </el-form-item>
        <el-form-item label="排查人员" prop="checkPeople">
          <el-input v-model="form.checkPeople" placeholder="请输入排查人员" />
        </el-form-item>
        <el-form-item label="是否排查" prop="isCheck">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否发现故障" prop="isFound">
          <el-select v-model="form.isFound" placeholder="请选择">
            <el-option
              v-for="item in isFoundOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.isFound === '1'" label="故障信息" prop="information">
          <el-input
            v-if="form.isFound === '1'"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.information">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/system/car";
import { ref, reactive, computed, watch, getCurrentInstance } from "vue";
export default {
  name: "Car",
  data() {
    return {
      // 屏幕宽高度
      screenWidth: 0,
      screenHeight: 0,
      // 是否打开pdf预览弹窗
      openPdf:false,
      // 是否检查
      statusOptions:[{
        label:'已排查',
        value:'1'
      },
        {
          label:'未排查',
          value: '0'
        }],
      // 是否发现故障
      isFoundOptions:[{
        label:'发现',
        value:'1'
      },
        {
          label:'未发现',
          value: '0'
        }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数据的车牌
      numbers: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        number: null,
        type: null,
        status: null,
        time: null,
        checkTeam: null,
        checkPeople: null,
        isFound: null,
        information: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // name: [
        //   { required: true, message: "项目名称不能为空", trigger: "blur" }
        // ],
        number: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "车型不能为空", trigger: "blur" }
        ],
        time: [
          { required: true, message: "排查时间不能为空", trigger: "blur" }
        ],
        checkTeam: [
          { required: true, message: "检查班组不能为空", trigger: "blur" }
        ],
        checkPeople: [
          { required: true, message: "排查人员不能为空", trigger: "blur" }
        ],
      }
    };
  },
  //监听从项目界面传过来的项目id,在未关闭页面时可以及时更新
  watch: {
    '$route.query.id': {
      handler(newVal, oldVal) {
        if(newVal !== undefined){
          this.queryParams.projectId = newVal
          //this.form.projectId = this.$route.query.id
          this.getList()
          console.log("新的值:" + newVal);
        }
      }
    }
  },
  created() {
    this.form.projectId = this.$route.query.id
    this.queryParams.projectId = this.$route.query.id
    this.getList();
  },
  mounted() {
    this.getScreenSize();
    console.log(this.screenWidth)
    console.log(this.screenHeight)

  },
  methods: {
    getScreenSize() {
      //获取屏幕宽高度
      this.screenWidth = window.innerWidth;
      this.screenHeight = window.innerHeight;
    },
    // 关闭pdf
    onClose(){
      this.openPdf = false
    },
    // 打开pdf
    openDetail(){
      console.log("打开pdf")
      this.openPdf = true
    },
    // 查询车辆列表
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        number: null,
        type: null,
        status: null,
        time: null,
        checkTeam: null,
        checkPeople: null,
        isFound: null,
        information: null
      };
      this.resetForm("form");
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 返回项目列表页面
    back(){
      this.$router.push('/project/view')
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆";
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCar(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆";
      });
    },
    // 提交按钮
    submitForm() {
      this.form.projectId = this.$route.query.id
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const plates = row.number ||this.numbers;
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车牌号为"' + plates + '"的数据项？').then(function() {
        return delCar(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 导出按钮操作
    handleExport() {
      this.download('system/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    }
  },

};
</script>
